jQuery(document).ready(function() {
	$('#kit-search-more').on('click', function() {
		$('.kit-search-mored').toggle();
	});

    function GetQueryString(name)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }


	var col = [ 
	{
	checkbox: 'checkbox'
	},
	{
		title:'id',
		field:'id',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'推荐记录的唯一标识',
		field:'uniqueId',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'推荐日期',
		field:'recommendDate',
		align: 'center',
		valign: 'middle'
	},
	{
		title:'推荐人id',
		field:'recommenderId',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'推荐人微信昵称',
		field:'recommenderWechatNickname',
		align: 'center',
		valign: 'middle'
	},
	{
		title:'推荐人',
		field:'recommenderName',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'推荐人电话',
		field:'recommenderPhone',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'推荐人Zoom账号',
		field:'recommenderZoomId',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'被推荐人id',
		field:'newStudentId',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'被推荐人英文名',
		field:'newStudentName',
		align: 'center',
		valign: 'middle'
	},
	{
		title:'被推荐人电话',
		field:'newStudentPhone',
		align: 'center',
		valign: 'middle'
	},
	{
		title:'被推荐人Zoom账号',
		field:'newStudentZoomId',
		align: 'center',
		valign: 'middle'
	},
	{
		title:'负责人id',
		field:'fuzerenId',
		align: 'center',
		valign: 'middle',
		visible: false
	},
	{
		title:'负责人',
		field:'fuzeren',
		align: 'center',
		valign: 'middle'
	},
        {
            title:'注册状态',
            field:'regStatus',
            align: 'center',
            valign: 'middle'
        },
        {
            title:'试听状态',
            field:'lisStatus',
            align: 'center',
            valign: 'middle'
        },
        {
            title:'付款状态',
            field:'feeStatus',
            align: 'center',
            valign: 'middle'
        },
        {
            title:'奖励发放状态',
            field:'jiangStatus',
            align: 'center',
            valign: 'middle'
        },
	// {
	// 	title:'推荐状态',
	// 	field:'recommendState',
	// 	align: 'center',
	// 	valign: 'middle'
	// },
	// {
	// 	title:'奖励（小时）',
	// 	field:'recommendAward',
	// 	align: 'center',
	// 	valign: 'middle'
	// },
	// {
	// 	title:'核销（小时）',
	// 	field:'substractAward',
	// 	align: 'center',
	// 	valign: 'middle'
	// },
	{
		title:'备注',
		field:'remark',
		align: 'center',
		valign: 'middle'
	},
	{
		title: '管理',
	    formatter: function (value, row, index) {
	    	console.log('row', row);
            if(row.jiangStatus=='已发放'){
                return '<a href="javascript:return false;" onclick="return false;" style="background-color:#999" class="layui-btn layui-btn-sm layui-btn" >奖励发放</a>';
            }
            else {
                return '<a href="javascript:;" onclick="addAward(\''+row.uniqueId+'\')" class="layui-btn layui-btn-sm layui-btn">奖励发放</a>'+
					   '<a href="javascript:;" onclick="rewardWriteOff(\''+row.uniqueId+'\')" class="layui-btn layui-btn-sm layui-btn">奖励核销</a>';

			}
		    // '<a href="javascript:;" onclick="substractAward(\''+row.uniqueId+'\')" class="layui-btn layui-btn-sm layui-btn">奖励核销</a>'+
		    // '<a href="javascript:;" onclick="nothingHappen(\''+row.uniqueId+'\')" class="layui-btn layui-btn-sm layui-btn">修改试听ID</a>';
	    } //自定义方法，添加操作按钮
	
	}];

	//------------------------------------------加载列表
	$('#_table').bootstrapTable({
		url:baseUrl+'/recommendMsgs?recommenderId='+GetQueryString("studentid"),
		'method': "GET",
		locale: 'zh-CN',
		detailView: false,
		sidePagination: "server", //服务端处理分页
		striped: true,//隔行变色
		undefinedText: "",
		pagination: true,
		singleSelect: false,
		pageSize: pageList[0],
		cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
		pageNumber: 1,
		pageList: pageList,
		search: false, //不显示 搜索框
		uniqueId: "uniqueId", //每一行的唯一标识，一般为主键列
		showColumns: true, //不显示下拉框（选择显示的列）
		undefinedText: '',
		columns: col,
		showRefresh: true,
		search: true,
		queryParams: function(params) {
			return {
				pageNum: this.pageNumber,
				pageSize: this.pageSize,
				sortOrder: params.order
			}
		},
		
	});

})
var check = function() {

}
var doDelete = function() {
	var rows = $('#_table').bootstrapTable('getSelections');
	if(rows == [] || rows == null || rows.length == 0) {
		parent.layer.alert('请选择要编辑的数据！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 2000
		});
		return;
	}
	if(rows.length != 1) {
		parent.layer.alert('请选择单条数据进行编辑！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 1800
		});
		return;
	}
	$.ajax({
		type: "delete",
		url:baseUrl+ '/recommendMsgs/' + rows[0].uniqueId,
		async: true,
		success: function(data) {
			alert(data.msg);
			$('#_table').bootstrapTable('refresh');
		}
	});
}
var insert = function() {
	parent.layer.open({
		//				title: false,
		maxmin: true,
		type: 2,
		area: modalArea,
		content: './views/form/RecommendMsgEdit.html',
		btn: ['保存', '取消'],
		yes: function(index, layero) {
			var data = $(layero).find("iframe")[0].contentWindow.callback_save();
			console.log(layero);
			$.ajax({
				url:baseUrl+ '/recommendMsgs',
				type: 'POST',
				data: data,
				success: function(data) {
					alert(data.msg);
					parent.layer.close(index);
					$('#_table').bootstrapTable('refresh');
				}
			});
		},
		btn2: function(index, layero) {
			//按钮【按钮二】的回调
			console.log(layero);
			//return false 开启该代码可禁止点击该按钮关闭
		}
	});
}

var edit = function() {
	var rows = $('#_table').bootstrapTable('getSelections');
	if(rows == [] || rows == null || rows.length == 0) {
		parent.layer.alert('请选择要编辑的数据！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 2000
		});
		return;
	}
	if(rows.length != 1) {
		parent.layer.alert('请选择单条数据进行编辑！', {
			skin: 'layui-layer-molv', //样式类名
			closeBtn: 0,
			anim: 4,
			time: 1800
		});
		return;
	}
	parent.layer.open({
		//				title: false,
		maxmin: true,
		type: 2,
		area: modalArea,
		content: './views/form/RecommendMsgEdit.html?uniqueId='+rows[0].uniqueId, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		btn: ['保存', '取消'],
		yes: function(index, layero) {
			var data = $(layero).find("iframe")[0].contentWindow.callback_save();
			console.log(layero);
			$.ajax({
				url:baseUrl+'/recommendMsgs/'+rows[0].uniqueId,
				type: 'POST',
				data: data,
				success: function(data) {
					alert(data.msg);
					parent.layer.close(index);
					$('#_table').bootstrapTable('refresh');
				}
			});
		},
		btn2: function(index, layero) {
			//按钮【按钮二】的回调
			alert(index);
			console.log(layero);
			//return false 开启该代码可禁止点击该按钮关闭
		}
	});
}

//2018-9-26 chenbin: 管理功能
function addAward(uniqueId) {
//	alert('奖励发放功能开发中……uniqueId:', uniqueId);
	parent.layer.open({
		maxmin: true,
		type: 2,
		title: '奖励发放',
		area: ['300px','300px'],
		content: './views/form/RecommendAwardAdd.html?uniqueId='+uniqueId, 
		btn: ['保存', '取消'],
//		success: function(layero, index){
//			// 2018-9-25 chenbin: 父页面向子页面传递数据
//			var body = parent.layer.getChildFrame('body',index);//建立父子联系
//			body.contents().find('#remark').val(comments);
//		}, 
		yes: function(index, layero) {
			var data = $(layero).find("iframe")[0].contentWindow.callback_save();						
			var postMsg=uniqueId+"&"+data;
			if(data==""){
				alert("请填写备注后保存");
				parent.layer.close(index);
				$('#_table').bootstrapTable('refresh');
				return;
			}
			$.ajax({
				url:baseUrl+ '/recommendMsgs/saveRemark/',
				type: 'POST',
				data:{
		            postMsg:postMsg
		        },
				success: function(data) {
					alert(data.msg);
					parent.layer.close(index);
					$('#_table').bootstrapTable('refresh');
				}
			});
		},
		btn2: function(index, layero) {
		}
	});
}

function rewardWriteOff(uniqueId) {
	$.ajax({
			   url: baseUrl + '/recommendMsgs/rewardWriteOff/'+uniqueId,
			   type: 'GET',
			   success: function (data) {
				   $('#_table').bootstrapTable('refresh');
			   }
		   });

}


function substractAward(uniqueId) {
//	alert('奖励核销功能开发中……uniqueId:', uniqueId);
	parent.layer.open({
		maxmin: true,
		type: 2,
		title: '奖励核销',
		area: ['300px','360px'],
		content: './views/form/RecommendAwardSub.html?uniqueId='+uniqueId, 
		btn: ['保存', '取消'],
//		success: function(layero, index){
//			// 2018-9-25 chenbin: 父页面向子页面传递数据
//			var body = parent.layer.getChildFrame('body',index);//建立父子联系
//			body.contents().find('#remark').val(comments);
//		}, 
		yes: function(index, layero) {
			var data = $(layero).find("iframe")[0].contentWindow.callback_save();						
			var postMsg=uniqueId+"&"+data;
			if(data==""){
				alert("请填写备注后保存");
				parent.layer.close(index);
				$('#_table').bootstrapTable('refresh');
				return;
			}
			$.ajax({
				url:baseUrl+ '/recommendMsgs/saveSubstract/',
				type: 'POST',
				data:{
		            postMsg:postMsg
		        },
				success: function(data) {
					alert(data.msg);
					parent.layer.close(index);
					$('#_table').bootstrapTable('refresh');
				}
			});
		},
		btn2: function(index, layero) {
		}
	});
}
// 2018-9-26 chenbin: 怎么修改试听Id？
function nothingHappen(uniqueId) {
	alert('修改试听ID功能开发中……uniqueId:', uniqueId);
}